package com.leizi.igym.controller.admin;

import com.leizi.igym.domain.Region;
import com.leizi.igym.enums.ReturnCodeEnum;
import com.leizi.igym.exception.BusinessException;
import com.leizi.igym.resp.Result;
import com.leizi.igym.service.RegionService;
import com.leizi.igym.util.RedisUtils;
import com.leizi.igym.utils.ThrowUtils;
import com.leizi.igym.vo.RegionVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author: leizi0110
 * @CreateTime: 2024-04-08  09:15
 * @Description:
 */

@RestController("adminRegionController")
@RequestMapping("/admin/region")
@Tag(name = "管理端-区域相关接口")
@Slf4j
public class RegionController {

    @Resource
    private RegionService regionService;

    @Operation(summary = "管理端：新增区域")
    @PostMapping
    public Result add(@RequestBody Region region) {
        log.info("管理端正在新增区域分类，参数为：{}", region);
        if (region == null) {
            throw new BusinessException(ReturnCodeEnum.PARAMS_ERROR);
        }
        boolean result = regionService.save(region);
        if (!result){
            throw new BusinessException(ReturnCodeEnum.OPERATION_ERROR,"新增失败！");
        }
        RegionVO regionVO = regionService.getByIdWithEquip(region.getId());
        RedisUtils.set("region::"+regionVO.getId(),regionVO);
        return Result.ok(result);
    }

    @Operation(summary = "管理端：删除区域")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        log.info("管理端正在删除区域，参数为：{}", id);
        if (id == null || id <= 0) {
            throw new BusinessException(ReturnCodeEnum.PARAMS_ERROR);
        }
        boolean b = regionService.removeById(id);
        if (b){
            RedisUtils.remove("region::"+id);
            return Result.ok(b);
        }
        return Result.fail(b);
    }

    @Operation(summary = "管理端：修改区域")
    @PutMapping("{id}")
    public Result updateRegion(@PathVariable("id") Long id,
                                 @RequestBody Region region) {
        log.info("管理端修改区域，参数为：{}", region);
        ThrowUtils.throwIf(Objects.isNull(region) ||
                        id == null || id <= 0 || !region.getId().equals(id)
                , ReturnCodeEnum.PARAMS_ERROR);
        boolean result = regionService.updateRegion(region);
        if (!result) {
            throw new BusinessException(ReturnCodeEnum.OPERATION_ERROR, "删除失败！");
        }
        return Result.ok(result);
    }

    @Operation(summary = "管理端：查询所有区域")
    @GetMapping
    public Result<List<Region>> getRegion() {
        return Result.ok(regionService.getBaseMapper().selectList(null));
    }

    @Operation(summary = "管理端：根据id查询区域")
    @GetMapping("{id}")
    public Result<RegionVO> getById(@PathVariable("id")Long id) {
        RegionVO regionVO = regionService.getByIdWithEquip(id);
        return Result.ok(regionVO);
    }
}
